package youcheng.work.view_zhexian;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {
    public Button Cancel;
    private EditText usr_id, pwd, pwd_again;
    private ImageView img_return;
    private MySqliteopenhelper mySqliteopenhelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        Cancel   = findViewById(R.id.bt_cancel);
        Button register = findViewById(R.id.bt_register_again);
        usr_id =findViewById(R.id.usrid_reg);
        pwd_again =findViewById(R.id.pwd_reg_again);
        pwd =findViewById(R.id.pwd_reg);
        img_return = findViewById(R.id.imgv_return);
        mySqliteopenhelper = new MySqliteopenhelper(this);

        TextView tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("注册");

            Cancel.setOnClickListener(v -> finish());

        register.setOnClickListener(v -> {
            String id        = usr_id.getText().toString().trim();
            String password  = pwd.getText().toString().trim();
            String pass_again= pwd_again.getText().toString().trim();
            User user = new User(id, password);

            if(usr_id.length() == 0||pwd.length() == 0)
            {
                Toast.makeText(RegisterActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                return ;
            }

            if(password.equals(pass_again))  {

                if (mySqliteopenhelper.register(user)!= -1) {
                    Toast.makeText(RegisterActivity.this, "注册成功,您现在可以直接登录", Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(RegisterActivity.this, "注册失败或用户名已存在，请重试", Toast.LENGTH_SHORT).show();
                }
            }
            else{
                Toast.makeText(RegisterActivity.this, "两次密码不同请重试", Toast.LENGTH_SHORT).show();
            }


        });

        img_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();/*返回上一个activity*/
            }
        });
    }

}